/**
 * 
 */
package renmopr.tourguide.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import renmopr.tourguide.dao.Photo;
import renmopr.tourguide.dao.Place;
import renmopr.tourguide.dao.PlaceNews;
import renmopr.tourguide.util.TextUtil;

/**
 * @author opunc
 */

public class WxController extends BaseController {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(WxController.class);
	
	public void index() {
	}
	
	/**
	 * 模糊查询地址
	 */
	public void getByWord() {
		Map<String, Object> map = new HashMap<String, Object>();
		String word = getPara("word");
		if (TextUtil.isEmpty(word)) {
			map.put("code", 30001);
			map.put("message", "参数不全");
			renderJson(map);
			return;
		}
		String[] param = word.split(" ");
		if (param != null && param.length > 0) {
			List<Object> conditionList = new ArrayList<Object>(param.length * 2);
			StringBuffer sb = new StringBuffer();
			sb.append(" and (name like ? or address like ?");
			conditionList.add("%" + param[0] + "%");
			conditionList.add("%" + param[0] + "%");
			for(int i=1; i<param.length; i++) {
				sb.append(" or name like ? or address like ?");
				conditionList.add("%" + param[i] + "%");
				conditionList.add("%" + param[i] + "%");
			}
			sb.append(")");
			List<Place> list = Place.dao.find("select id, lat, `long`, `type`, name, address, disctrict, img_url, `desc` from place where 1 = 1" + sb.toString(), conditionList.toArray());
			if (list != null && list.size() >0) {
				map.put("list", list);
			}
		}
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(map);
	}
	
	/**
	 * 查找附近景点
	 */
	public void getAround() {
		Map<String, Object> map = new HashMap<String, Object>();
		String lon = getPara("long");
		String lat = getPara("lat");
		if (TextUtil.isEmpty(lon) || TextUtil.isEmpty(lat)) {
			map.put("code", 30001);
			map.put("message", "参数不全");
			renderJson(map);
			return;
		}
		int d = 0;
		for (int i=0; i< 3; i++) {
			d = i*2+1;
			List<Place> l = Place.dao.find("SELECT id, lat, `long`, `type`, name, address, disctrict, img_url, `desc` FROM place WHERE MBRContains ( LineString ( Point ( ? + ? / ( 111.1 / COS(RADIANS(?)) ), ? + ? / 111.1 ), Point ( ? - ? / ( 111.1 / COS(RADIANS(?)) ), ? - ? / 111.1 ) ), geom )", lat,d,lon,lon,d,lat,d,lon,lon,d);
			// List<Place> l = Place.dao.find("SELECT id, lat, `long`, `type`, name, address, disctrict, img_url, `desc` FROM place WHERE MBRContains ( LineString ( Point ( ? + 1 / ( 111.1 / COS(RADIANS(?)) ), ? + 1 / 111.1 ), Point ( ? - 1 / ( 111.1 / COS(RADIANS(?)) ), ? - 1 / 111.1 ) ), geom )", lat,lon,lon,lat,lon,lon);
			if (l != null && l.size() >0) {
				map.put("list", l);
				break;
			}
		}

		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(map);
	}
	
	/**
	 * type：全部|优秀历史建筑|红色旅游景点
	 */
	public void getByType() {
		Map<String, Object> map = new HashMap<String, Object>();
		String type = getPara("type");
		if (TextUtil.isEmpty(type)) {
			map.put("code", 30001);
			map.put("message", "参数不全");
			renderJson(map);
			return;
		}
		List<Place> l = null;
		if ("全部".equals(type)) {
			l = Place.dao.findAll();
		} else {
			l = Place.dao.find("select id, lat, `long`, `type`, name, address, disctrict, img_url, `desc` from place where type = ?", type);
		}
		if (l != null && l.size() >0) {
			map.put("list", l);
		}
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(map);
	}
	
	/**
	 * disc：嘉定|奉贤|宝山|崇明|徐汇|普陀|杨浦|松江|浦东|虹口|金山|长宁|闵行|青浦|静安|黄浦
	 */
	public void getByDisctrict() {
		Map<String, Object> map = new HashMap<String, Object>();
		String disc = getPara("disc");
		if (TextUtil.isEmpty(disc)) {
			map.put("code", 30001);
			map.put("message", "参数不全");
			renderJson(map);
			return;
		}
		List<Place> l = Place.dao.find("select id, lat, `long`, `type`, name, address, disctrict, img_url, `desc` from place where disctrict = ?", disc);
		if (l != null && l.size() >0) {
			map.put("list", l);
		}
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(map);
	}
	
	/**
	 * 获取地址具体信息
	 */
	public void getById() {
		Map<String, Object> map = new HashMap<String, Object>();
		int id = getParaToInt("id");
		Place p = Place.dao.findById(id);
		if (p != null) {
			map.put("id", p.getInt("id"));
			map.put("name", p.getStr("name"));
			map.put("disctrict", p.getStr("disctrict"));
			map.put("address", p.getStr("address"));
			map.put("long", p.getStr("long"));
			map.put("lat", p.getStr("lat"));
			map.put("desc", p.getStr("desc"));
			map.put("img_url", p.getStr("img_url"));
			map.put("type", p.getStr("type"));
		}
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(map);
	}
	
	/**
	 * 获取年代的地址
	 */
	public void getAge() {
		Map<String, Object> map = new HashMap<String, Object>();
		int age = getParaToInt("age");
		List<Place> list = Place.dao.find("select id, lat, `long`, `type`, name, address, disctrict, img_url, `desc` from place where age = ?", age);
		if (list != null && list.size()> 0) {
			map.put("list", list);
		}
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(map);
	}

	public void listNews() {
		Map<String, Object> map = new HashMap<String, Object>();
		int placeId = getParaToInt("place_id");
		int type = getParaToInt("type");
		List<PlaceNews> list;
		if (type == 0) {
			list = PlaceNews.dao.find("select * from place_news where place_id=? and type=? order by id", placeId, "解放日报");
		} else {
			list = PlaceNews.dao.find("select * from place_news where place_id=? and type<>? order by id", placeId, "解放日报");
		}
		map.put("list", list);
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(JsonKit.toJson(map));
	}

	public void listPhoto() {
		Map<String, Object> map = new HashMap<String, Object>();
		int placeId = getParaToInt("place_id");
		List<Photo> list = Photo.dao.find("select * from photo where place_id=? order by sort_order asc", placeId);

		map.put("list", list);
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(JsonKit.toJson(map));
	}

	public void news() {
		Map<String, Object> map = new HashMap<String, Object>();
		int id = getParaToInt("id");
		PlaceNews placeNews = PlaceNews.dao.findById(id);

		map.put("data", placeNews);
		map.put("code", 20000);
		map.put("message", "SUCCESS");
		renderJson(JsonKit.toJson(map));
	}
}
